package com.mangowang.module.main.ui;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.utils.TextUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.help.GlideHelper;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.AgentPolicy;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.lib.common.util.StringUtil;
import com.mangowang.lib.common.view.NumberRunningTextView;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.other.LevelChangView;
import com.ruffian.library.widget.RRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class LevelActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R2.id.iv_level)
    ImageView ivLevel;
    @BindView(R2.id.tv_name)
    TextView tvName;

    @BindView(R2.id.tv_kao_cheng_text)
    TextView tvKaoChengText;
    @BindView(R2.id.tv_ju_li)
    TextView tvJuLi;
    @BindView(R2.id.tv_kao_time)
    TextView tvKaoTime;

    @BindView(R2.id.rl_kuang)
    RRelativeLayout rlKuang;
    @BindView(R2.id.sfly)
    FrameLayout sfly;
    @BindView(R2.id.id_viewpager)
    ViewPager idViewpager;
    @BindView(R2.id.lc_view)
    LevelChangView lcView;
    @BindView(R2.id.tv_phone)
    TextView tvPhone;




    final List<Fragment> fragmentList = new ArrayList<>();
    @BindView(R2.id.tvNumberView)
    NumberRunningTextView tvNumberView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setTranslucentStatus(this);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("升级攻略");
        initData();
        backIv.setOnClickListener(v -> finish());
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


    private void initData() {
        NetworkManager.INSTANCE.post(Apis.get_levels_instructions, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                AgentPolicy.get_levels_instructions datas = AgentPolicy.get_levels_instructions.parseFrom(response);
                SetData(datas);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });


    }

    private void SetData(AgentPolicy.get_levels_instructions datas) {
        Log.e("get_levels_instructions", datas.toString());
//        ivLevel.setBackgroundResource(R.mipmap.icon_levle_0);
        GlideHelper.INSTANCE.loadImage(this, ivLevel, UserHelper.getUser().getMember_avatar());
        tvName.setText("Hi " + datas.getMemberName());
        tvKaoChengText.setText(datas.getMsg());
        tvNumberView.setContent(datas.getAmount());
        tvPhone.setText(StringUtil.PhoneToPass(UserHelper.getUser().getMember_mobile()));
        if (TextUtils.isEmpty(datas.getDate())) {
            tvKaoTime.setText("0");
        } else {
            tvKaoTime.setText(datas.getDate());
        }
        lcView.setData(1, Integer.parseInt(datas.getLevel()), datas.getLengthList(), datas.getAmount(), datas.getFloat());

        for (int i = 0; i < datas.getLengthList().size(); i++) {
            Fragment firstFragment = new ImageFragment();
            Bundle bundle = new Bundle();
            bundle.putString("pic", datas.getLengthList().get(i).getPic());
            firstFragment.setArguments(bundle);
            fragmentList.add(firstFragment);
        }
        idViewpager.setPageMargin(20);
        idViewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return null;
            }

        });
        final int with = getPMWith() / 3;
        idViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int marginX = (int) ((position + positionOffset) * with);
                if (sfly.getScrollX() != marginX) sfly.scrollTo(marginX, 0);
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        int leve = Integer.parseInt(datas.getLevel()) - 1;
        if (leve < 0) leve = 0;
        idViewpager.setCurrentItem(leve);

    }

    private int getPMWith() {
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

}
